﻿package mortal.game.view.ranking
{
    import Message.Public.*;
    import com.mui.utils.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.ranking.view.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class RankingModule extends Window
    {
        private var _rankingSearchSpr:RankingSearchSpr;
        private var _rankingCategorySpr:RankingCategorySpr;
        private var _rankingFirstInfoSpr:RankingFirstInfoSpr;
        private var _rankingDataInfoSpr:RankingDataInfoSpr;
        private var _ranking3DdisplaySpr:Model3DdisplayPanelBase;
        private var _currRankType:int;
        private var _rect3d:Rect3DObject;

        public function RankingModule(param1:ILayer = null)
        {
            super(param1);
            setSize(941, 505);
            this.layer = LayerManager.windowLayer3D;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            _titleSpriteHight = 60;
            this.pushUIToDisposeVec(UIFactory.bg(0, 0, 925 + 8, 608 + 3, this, ImagesConst.WindowBg3));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.RankingTitleDecorate, 353 - 23, 8 - 44, this));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.RankingTitle, 422, 46 - 25, this));
            this.pushUIToDisposeVec(UIFactory.bg(10, 64 - 5, 176 + 3, 542, this, ImagesConst.PanelBg3));
            this._rankingCategorySpr = UICompomentPool.getUICompoment(RankingCategorySpr);
            this._rankingCategorySpr.x = 11 + 7;
            this._rankingCategorySpr.y = 65 - 1;
            this.addChild(this._rankingCategorySpr);
            this.pushUIToDisposeVec(UIFactory.bg((188 + 1), 64 - 5, 460 + 3, 542, this, ImagesConst.PanelBg3));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.RankingInnerBg, 188 + 4, 94 - 2, this));
            this._rankingSearchSpr = UICompomentPool.getUICompoment(RankingSearchSpr);
            this._rankingSearchSpr.x = 17 + 148;
            this._rankingSearchSpr.y = 30 + 40 - 3;
            this.addChild(this._rankingSearchSpr);
            this.pushUIToDisposeVec(UIFactory.bg(651 + 2, 64 - 5, 266 + 4, 542, this, ImagesConst.PanelBg3));
            this._ranking3DdisplaySpr = UICompomentPool.getUICompoment(RankingEquipDisplaySpr, this);
            this._ranking3DdisplaySpr.x = 664 + 14 - 16;
            this._ranking3DdisplaySpr.y = 32 + 38;
            this.contentTopOf3DSprite.addChild(this._ranking3DdisplaySpr);
            this._rankingFirstInfoSpr = UICompomentPool.getUICompoment(RankingFirstInfoSpr);
            this._rankingFirstInfoSpr.x = 212 - 12;
            this._rankingFirstInfoSpr.y = 75 + 80 - 62;
            this.addChild(this._rankingFirstInfoSpr);
            this.addListeners();
            this.getRankingFirstInfoHandler();
            return;
        }// end function

        private function addListeners() : void
        {
            Dispatcher.addEventListener(EventName.RankingGetRankList, this.getRankingListHandler);
            Dispatcher.addEventListener(EventName.RankingGetFirstInfo, this.getRankingFirstInfoHandler);
            NetDispatcher.addCmdListener(ServerCommand.RankingGetRankListSucc, this.getRankingListSuccHandler);
            return;
        }// end function

        private function removeListeners() : void
        {
            Dispatcher.removeEventListener(EventName.RankingGetRankList, this.getRankingListHandler);
            Dispatcher.removeEventListener(EventName.RankingGetFirstInfo, this.getRankingFirstInfoHandler);
            NetDispatcher.removeCmdListener(ServerCommand.RankingGetRankListSucc, this.getRankingListSuccHandler);
            return;
        }// end function

        override protected function updateBtnSize() : void
        {
            if (_closeBtn)
            {
                _closeBtn.x = this.width - _closeBtn.width - 5 - 33;
                _closeBtn.y = 5 + 19;
            }
            return;
        }// end function

        public function switchRankingDisplayType(param1:int) : void
        {
            if (param1 == 0)
            {
                if (this._rankingDataInfoSpr)
                {
                    this._rankingDataInfoSpr.dispose(true);
                    this._rankingDataInfoSpr = null;
                }
                if (!this._rankingFirstInfoSpr)
                {
                    this._rankingFirstInfoSpr = UICompomentPool.getUICompoment(RankingFirstInfoSpr);
                    this._rankingFirstInfoSpr.x = 212 - 12;
                    this._rankingFirstInfoSpr.y = 75 + 80 - 62;
                    this.addChild(this._rankingFirstInfoSpr);
                }
            }
            else
            {
                if (this._rankingFirstInfoSpr)
                {
                    this._rankingFirstInfoSpr.dispose(true);
                    this._rankingFirstInfoSpr = null;
                }
                if (!this._rankingDataInfoSpr)
                {
                    this._rankingDataInfoSpr = UICompomentPool.getUICompoment(RankingDataInfoSpr);
                    this._rankingDataInfoSpr.x = 202 - 10;
                    this._rankingDataInfoSpr.y = 64 + 29;
                    this.addChild(this._rankingDataInfoSpr);
                }
            }
            return;
        }// end function

        private function getRankingListHandler(event:DataEvent) : void
        {
            var _loc_3:Array = null;
            var _loc_2:* = event.data;
            this.switchRankingDisplayType(1);
            if (this._currRankType != _loc_2.type)
            {
                this.rankingDataInfoSpr.clear();
                this.ranking3DdisplaySpr.clear();
                this._currRankType = _loc_2.type as int;
                this.rankingDataInfoSpr.currRankType = _loc_2.type as int;
                this.rankingDataInfoSpr.updataHeadInfo();
                if (_loc_2.type == 404)
                {
                    return;
                }
                _loc_3 = Cache.instance.ranking.getRankingDataByType(_loc_2.type);
                if (!_loc_3 || _loc_3[0] && _loc_3[0].version < Cache.instance.ranking.latestVersion)
                {
                    GameProxy.ranking.getToplist(_loc_2.type);
                }
                else
                {
                    this.rankingDataInfoSpr.updateAllInfo(_loc_3);
                }
            }
            return;
        }// end function

        private function getRankingFirstInfoHandler(event:DataEvent = null) : void
        {
            if (this._currRankType != EToplistType._EToplistTypePlayerOverview)
            {
                this.switchRankingDisplayType(0);
                this._currRankType = EToplistType._EToplistTypePlayerOverview;
                this.ranking3DdisplaySpr.clear();
            }
            var _loc_2:* = Cache.instance.ranking.getRankingDataByType(EToplistType._EToplistTypePlayerOverview);
            if (!_loc_2 || _loc_2[0] && _loc_2[0].version < Cache.instance.ranking.latestVersion)
            {
                GameProxy.ranking.getToplist(EToplistType._EToplistTypePlayerOverview);
            }
            else
            {
                this._rankingFirstInfoSpr.data = _loc_2;
            }
            return;
        }// end function

        private function getRankingListSuccHandler(param1:Object) : void
        {
            var _loc_2:Array = null;
            if (param1)
            {
                _loc_2 = param1.rankingList;
                if (_loc_2.length > 0)
                {
                    Cache.instance.ranking.saveRankingData(_loc_2[0].toplistType, _loc_2);
                    if (_loc_2[0].toplistType > 0 && this.rankingDataInfoSpr)
                    {
                        this.rankingDataInfoSpr.updateAllInfo(_loc_2);
                    }
                    else if (_loc_2[0].toplistType == 0 && this._rankingFirstInfoSpr)
                    {
                        this._rankingFirstInfoSpr.data = _loc_2;
                    }
                }
            }
            return;
        }// end function

        public function get rect3d() : Rect3DObject
        {
            return this._rect3d;
        }// end function

        public function set rect3d(param1:Rect3DObject) : void
        {
            this._rect3d = param1;
            return;
        }// end function

        public function get rankingDataInfoSpr() : RankingDataInfoSpr
        {
            return this._rankingDataInfoSpr;
        }// end function

        public function get rankingFirstInfoSpr() : RankingFirstInfoSpr
        {
            return this._rankingFirstInfoSpr;
        }// end function

        public function get ranking3DdisplaySpr() : Model3DdisplayPanelBase
        {
            return this._ranking3DdisplaySpr;
        }// end function

        public function get currRankType() : int
        {
            return this._currRankType;
        }// end function

        public function set currRankType(param1:int) : void
        {
            this._currRankType = param1;
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._rankingSearchSpr.dispose(param1);
            this._rankingCategorySpr.dispose(param1);
            if (this._rankingFirstInfoSpr)
            {
                this._rankingFirstInfoSpr.dispose(param1);
            }
            if (this._rankingDataInfoSpr)
            {
                this._rankingDataInfoSpr.dispose(param1);
            }
            this._ranking3DdisplaySpr.dispose(param1);
            this._rankingSearchSpr = null;
            this._rankingCategorySpr = null;
            this._rankingFirstInfoSpr = null;
            this._rankingDataInfoSpr = null;
            this._ranking3DdisplaySpr = null;
            this._currRankType = 0;
            if (this._rect3d)
            {
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
            }
            this.removeListeners();
            return;
        }// end function

    }
}
